import 'dart:io';
import 'package:word_game/task/kill_monster_task.dart';
import 'package:word_game/task/kill_monster_task_expecta.dart';
import 'package:word_game/task/task_core.dart';
import 'package:yaml/yaml.dart';

import 'package:word_game/task/task.dart';

class TaskManager extends Object {
  static TaskManager instance = TaskManager();

  Map<String, Task> taskMap={};

  appendTasksFromYAML(String fileName) {
    var content = File(fileName).readAsStringSync();
    var yaml = loadYaml(content);
    var items = yaml['tasks'] as List;
    for (var item in items) {
      Task obj = Task('');
      if (item['type']=='get_thing') {
        obj = Task('');
        obj.expecta = ThingTaskExpecta();
        obj.thingCode = item['thingCode'];
      } else if(item['type']=='kill_monster') {
        obj = KillMonsterTask('');
        var kmTask = obj as KillMonsterTask;
        for (YamlMap monsterInfo in item['monsterInfo']) {
          Map<String, String> info = {};
          info['code'] = monsterInfo['code'] as String;
          info['number'] = monsterInfo['number'] as String;
          kmTask.monsterInfo.add(info);
        }
        obj.expecta = KillMonsterTaskExpecta();
      }
      obj.code = item['code'];
      obj.name = item['name'];
      obj.desc = item['desc'];
      obj.initThingCode = item['initThingCode'];
      obj.require = item['require'];
      obj.rewardThingCode = item['rewardThingCode'];
      
      taskMap[obj.code] = obj;
    }
  }

  Task taskWithCode(String code) {
    return taskMap[code];
  }

}